﻿using System.Linq;
using System.Linq.Expressions;

namespace Fuse8.DomainFramework.Common
{
	internal static class ExpressionExtensions
	{
		public static bool HasParameter (this Expression expression)
		{
			if (expression is UnaryExpression)
			{
				return ((UnaryExpression) expression).Operand.HasParameter();
			}
			if (expression is BinaryExpression)
			{
				var binaryExpression = ((BinaryExpression) expression);
				return binaryExpression.Left.HasParameter() || binaryExpression.Right.HasParameter();
			}
			if (expression is MemberExpression)
			{
				return ((MemberExpression) expression).Expression.HasParameter();
			}
			if (expression is MethodCallExpression)
			{
				var methodCallExpression = ((MethodCallExpression)expression);
				return methodCallExpression.Arguments.Aggregate(methodCallExpression.Object.HasParameter(), (current, argument) => current || argument.HasParameter());
			}
			return expression is ParameterExpression;
		}
	}
}